
<template>
  <div class="headreg">
    <van-nav-bar
      left-arrow
      title="注册"
      @click-left="$router.back('/login')"
    ></van-nav-bar>

    <van-form @submit="onSubmit" ref="regForm">
      <!-- field 字段 -->
      <van-field
        v-model="username"
        name="username"
        clearable
        label="手机号"
        placeholder="请填写手机号"
        :rules="[
          { required: true, message: '请填写手机号' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误' },
        ]"
      />

      <van-field
        v-model="vercode"
        name="vercode"
        clearable
        label="验证码"
        placeholder="请填写验证码"
        :rules="[
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' },
        ]"
      >
        <template #button>
          <van-button
            size="small"
            type="primary"
            @click="sendSms"
            native-type="button"
            :disabled="codeBtnDisabled"
            >{{ code_text }}</van-button
          >
        </template>
      </van-field>

      <van-field
        v-model="password"
        name="password"
        clearable
        label="密码"
        placeholder="请填写密码"
        :rules="[
          { required: true, message: '请填写密码' },
          {
            validator: (e) => /^\w{6,12}$/.test(e),
            message: '密码格式错误,需要是6-12位的字母或数字',
          },
        ]"
      />

      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>
import { reg, sendSms, delete_user } from "@/api/user";
import { Form } from "vant";
import { Field } from "vant";
import { Button } from "vant";
export default {
  data() {
    return {
      user: [],
      show: true,
      username: "",
      vercode: "",
      password: "",
      codeBtnDisabled: false,
      code_text: "发送验证码",
    };
  },

  components: {
    [Field.name]: Field,
    [Form.name]: Form,
    [Button.name]: Button,
  },
  mounted() {
    // delete_user({
    //   username: "18135655263",
    //   password: "000000",
    // }).then((res) => {
    //   console.log(res);
    // });
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);

      reg(values).then((res) => {
        console.log(res);
      });
    },
    async sendSms() {
      try {
        let validate_res = await this.$refs.regForm.validate("username");
        //准备请求接口
        this.codeBtnDisabled = true;
        this.code_text = "发送中...";

        sendSms({
          mobile: this.username,
          type: "register",
        })
          .then((res) => {
            console.log(res);
            if (res.code == 0) {
              //发送成功

              this.$toast(`短信已发送到手机号${this.username},请注意查收`);
              let count = 15;
              let timer = setInterval(() => {
                count--;
                this.code_text = `${count}s后再次获取`;

                if (count <= 0) {
                  clearInterval(timer);
                  this.code_text = `再次获取`;
                  this.codeBtnDisabled = false;
                  count = 60;
                }
              }, 1000);
            } else {
              this.$toast(res.msg || "发送失败");
              this.codeBtnDisabled = false;
              this.code_text = "再次发送";
            }
          })
          .catch((err) => {
            console.log(err);

            this.$toast(err.msg || "发送失败");
            this.codeBtnDisabled = false;
            this.code_text = "再次发送";
          });
      } catch (e) {}
    },
  },
};
</script>
<style lang="less">
.headreg {
  background-color: @color3;
  height: @height;

  .van-button--round {
    border-radius: 0;
  }
  .van-button--info {
    background-color: @colorn;
    border: 1px solid @colorn;
    border-radius: 5px;
  }
  .van-button--small {
    height: 6.53333vw;
  }
  .van-button--primary {
    color: @color5;
    background-color: @color4;
    border: 1px solid @color4;
    border-radius: 20px;
  }
}
</style>